import os.path
import numpy as np
import pandas as pd
from tools.utils import load_config
from tools.utils import get_chinese_name_to_ts_code
from tools.utils import download_wind_edb
from tools.utils import loc_collection
from WindPy import w

cfg = load_config()
output_li = []
for exchange, exe_dic in cfg['exchange'].items():
    for chinese_name, ts_li in exe_dic.items():
        temp_dic = {}
        if not w.isconnected():
            w.start()
        ts_code = ts_li[1]
        symbol_ = ts_code.split("_")[0].upper()
        # data = w.wsd(f"{symbol_}FI.WI", "open,high,low,close,volume,amount",
        #              "2002-01-01", "2022-01-14", "unit=1")
        # if data.ErrorCode == 0:
        #     df_dic = {'index': data.Times, 'data': data.Data, 'columns': data.Fields}
        #     print(f"抓取{symbol_}FI.WI成功,from{data.Times[0]},")
        # else:
        #     print(f"抓取{symbol_}FI.WI失败，错误码{data.ErrorCode}")
        # output_li.append(temp_dic)
        for field in ['open', 'high', 'low', 'close', 'volume', 'amount']:
            dic = {'name': chinese_name, 'codes': f"{symbol_}FI.WI", 'fields': field, 'options': "unit=1",
                   'dataset_name': 'y_wi_fu_code', 'alias': chinese_name, 'wind_code': ts_li[0], 'symbol': symbol_,
                   'to_be_trained': False, 'to_be_inferred': False}
            output_li.append(dic)
d = pd.DataFrame(output_li)